<?php
/*
	This SQL query will create the table to store your object.

	CREATE TABLE `wiki_page` (
	`wiki_pageid` int(11) NOT NULL auto_increment,
	`wiki_id` INT NOT NULL,
	`title` TEXT NOT NULL,
	`wiki_namespaceid` int(11) NOT NULL,
	`creation_date` VARCHAR(255) NOT NULL,
	`deleted` TINYINT NOT NULL,
	`last_edit` VARCHAR(255) NOT NULL,
	`update_timestamp` INT NOT NULL, INDEX(`wiki_namespaceid`), PRIMARY KEY  (`wiki_pageid`)) ENGINE=MyISAM;
*/

/**
* <b>wiki_page</b> class with integrated CRUD methods.
* @author Php Object Generator
* @version POG 3.0f / PHP5
* @copyright Free for personal & commercial use. (Offered under the BSD license)
* @link http://www.phpobjectgenerator.com/?language=php5&wrapper=pog&objectName=wiki_page&attributeList=array+%28%0A++0+%3D%3E+%27wiki_id%27%2C%0A++1+%3D%3E+%27title%27%2C%0A++2+%3D%3E+%27wiki_namespace%27%2C%0A++3+%3D%3E+%27creation_date%27%2C%0A++4+%3D%3E+%27deleted%27%2C%0A++5+%3D%3E+%27last_edit%27%2C%0A++6+%3D%3E+%27update_timestamp%27%2C%0A++7+%3D%3E+%27views_on_page_at_tick%27%2C%0A++8+%3D%3E+%27diff_record%27%2C%0A%29&typeList=array+%28%0A++0+%3D%3E+%27INT%27%2C%0A++1+%3D%3E+%27TEXT%27%2C%0A++2+%3D%3E+%27BELONGSTO%27%2C%0A++3+%3D%3E+%27VARCHAR%28255%29%27%2C%0A++4+%3D%3E+%27TINYINT%27%2C%0A++5+%3D%3E+%27VARCHAR%28255%29%27%2C%0A++6+%3D%3E+%27INT%27%2C%0A++7+%3D%3E+%27HASMANY%27%2C%0A++8+%3D%3E+%27HASMANY%27%2C%0A%29
*/
include_once('class.pog_base.php');
class wiki_page extends POG_Base
{
	public $wiki_pageId = '';

	/**
	 * @var INT
	 */
	public $wiki_id;
	
	/**
	 * @var TEXT
	 */
	public $title;
	
	/**
	 * @var INT(11)
	 */
	public $wiki_namespaceId;
	
	/**
	 * @var VARCHAR(255)
	 */
	public $creation_date;
	
	/**
	 * @var TINYINT
	 */
	public $deleted;
	
	/**
	 * @var VARCHAR(255)
	 */
	public $last_edit;
	
	/**
	 * @var INT
	 */
	public $update_timestamp;
	
	/**
	 * @var private array of views_on_page_at_tick objects
	 */
	private $_views_on_page_at_tickList = array();
	
	/**
	 * @var private array of diff_record objects
	 */
	private $_diff_recordList = array();
	
	public $pog_attribute_type = array(
		"wiki_pageId" => array('db_attributes' => array("NUMERIC", "INT")),
		"wiki_id" => array('db_attributes' => array("NUMERIC", "INT")),
		"title" => array('db_attributes' => array("TEXT", "TEXT")),
		"wiki_namespace" => array('db_attributes' => array("OBJECT", "BELONGSTO")),
		"creation_date" => array('db_attributes' => array("TEXT", "VARCHAR", "255")),
		"deleted" => array('db_attributes' => array("NUMERIC", "TINYINT")),
		"last_edit" => array('db_attributes' => array("TEXT", "VARCHAR", "255")),
		"update_timestamp" => array('db_attributes' => array("NUMERIC", "INT")),
		"views_on_page_at_tick" => array('db_attributes' => array("OBJECT", "HASMANY")),
		"diff_record" => array('db_attributes' => array("OBJECT", "HASMANY")),
		);
	public $pog_query;
	
	
	/**
	* Getter for some private attributes
	* @return mixed $attribute
	*/
	public function __get($attribute)
	{
		if (isset($this->{"_".$attribute}))
		{
			return $this->{"_".$attribute};
		}
		else
		{
			return false;
		}
	}
	
	function wiki_page($wiki_id='', $title='', $creation_date='', $deleted='', $last_edit='', $update_timestamp='')
	{
		$this->wiki_id = $wiki_id;
		$this->title = $title;
		$this->creation_date = $creation_date;
		$this->deleted = $deleted;
		$this->last_edit = $last_edit;
		$this->update_timestamp = $update_timestamp;
		$this->_views_on_page_at_tickList = array();
		$this->_diff_recordList = array();
	}
	
	
	/**
	* Gets object from database
	* @param integer $wiki_pageId 
	* @return object $wiki_page
	*/
	function Get($wiki_pageId)
	{
		$connection = Database::Connect();
		$this->pog_query = "select * from `wiki_page` where `wiki_pageid`='".intval($wiki_pageId)."' LIMIT 1";
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$this->wiki_pageId = $row['wiki_pageid'];
			$this->wiki_id = $this->Unescape($row['wiki_id']);
			$this->title = $this->Unescape($row['title']);
			$this->wiki_namespaceId = $row['wiki_namespaceid'];
			$this->creation_date = $this->Unescape($row['creation_date']);
			$this->deleted = $this->Unescape($row['deleted']);
			$this->last_edit = $this->Unescape($row['last_edit']);
			$this->update_timestamp = $this->Unescape($row['update_timestamp']);
		}
		return $this;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $wiki_pageList
	*/
	function GetList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$connection = Database::Connect();
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$this->pog_query = "select * from `wiki_page` ";
		$wiki_pageList = Array();
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " where ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($this->pog_attribute_type[$sortBy]['db_attributes']) && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE($sortBy) ";
				}
				else
				{
					$sortBy = "$sortBy ";
				}
			}
			else
			{
				$sortBy = "$sortBy ";
			}
		}
		else
		{
			$sortBy = "wiki_pageid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$thisObjectName = get_class($this);
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$wiki_page = new $thisObjectName();
			$wiki_page->wiki_pageId = $row['wiki_pageid'];
			$wiki_page->wiki_id = $this->Unescape($row['wiki_id']);
			$wiki_page->title = $this->Unescape($row['title']);
			$wiki_page->wiki_namespaceId = $row['wiki_namespaceid'];
			$wiki_page->creation_date = $this->Unescape($row['creation_date']);
			$wiki_page->deleted = $this->Unescape($row['deleted']);
			$wiki_page->last_edit = $this->Unescape($row['last_edit']);
			$wiki_page->update_timestamp = $this->Unescape($row['update_timestamp']);
			$wiki_pageList[] = $wiki_page;
		}
		return $wiki_pageList;
	}
	
	
	/**
	* Saves the object to the database
	* @return integer $wiki_pageId
	*/
	function Save($deep = true)
	{
		$connection = Database::Connect();
		$this->pog_query = "select `wiki_pageid` from `wiki_page` where `wiki_pageid`='".$this->wiki_pageId."' LIMIT 1";
		$rows = Database::Query($this->pog_query, $connection);
		if ($rows > 0)
		{
			$this->pog_query = "update `wiki_page` set 
			`wiki_id`='".$this->Escape($this->wiki_id)."', 
			`title`='".$this->Escape($this->title)."', 
			`wiki_namespaceid`='".$this->wiki_namespaceId."', 
			`creation_date`='".$this->Escape($this->creation_date)."', 
			`deleted`='".$this->Escape($this->deleted)."', 
			`last_edit`='".$this->Escape($this->last_edit)."', 
			`update_timestamp`='".$this->Escape($this->update_timestamp)."'where `wiki_pageid`='".$this->wiki_pageId."'";
		}
		else
		{
			$this->pog_query = "insert into `wiki_page` (`wiki_id`, `title`, `wiki_namespaceid`, `creation_date`, `deleted`, `last_edit`, `update_timestamp`) values (
			'".$this->Escape($this->wiki_id)."', 
			'".$this->Escape($this->title)."', 
			'".$this->wiki_namespaceId."', 
			'".$this->Escape($this->creation_date)."', 
			'".$this->Escape($this->deleted)."', 
			'".$this->Escape($this->last_edit)."', 
			'".$this->Escape($this->update_timestamp)."')";
		}
		$insertId = Database::InsertOrUpdate($this->pog_query, $connection);
		if ($this->wiki_pageId == "")
		{
			$this->wiki_pageId = $insertId;
		}
		if ($deep)
		{
			foreach ($this->_views_on_page_at_tickList as $views_on_page_at_tick)
			{
				$views_on_page_at_tick->wiki_pageId = $this->wiki_pageId;
				$views_on_page_at_tick->Save($deep);
			}
			foreach ($this->_diff_recordList as $diff_record)
			{
				$diff_record->wiki_pageId = $this->wiki_pageId;
				$diff_record->Save($deep);
			}
		}
		return $this->wiki_pageId;
	}
	
	
	/**
	* Clones the object and saves it to the database
	* @return integer $wiki_pageId
	*/
	function SaveNew($deep = false)
	{
		$this->wiki_pageId = '';
		return $this->Save($deep);
	}
	
	
	/**
	* Deletes the object from the database
	* @return boolean
	*/
	function Delete($deep = false, $across = false)
	{
		if ($deep)
		{
			$views_on_page_at_tickList = $this->GetViews_on_page_at_tickList();
			foreach ($views_on_page_at_tickList as $views_on_page_at_tick)
			{
				$views_on_page_at_tick->Delete($deep, $across);
			}
			$diff_recordList = $this->GetDiff_recordList();
			foreach ($diff_recordList as $diff_record)
			{
				$diff_record->Delete($deep, $across);
			}
		}
		$connection = Database::Connect();
		$this->pog_query = "delete from `wiki_page` where `wiki_pageid`='".$this->wiki_pageId."'";
		return Database::NonQuery($this->pog_query, $connection);
	}
	
	
	/**
	* Deletes a list of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param bool $deep 
	* @return 
	*/
	function DeleteList($fcv_array, $deep = false, $across = false)
	{
		if (sizeof($fcv_array) > 0)
		{
			if ($deep || $across)
			{
				$objectList = $this->GetList($fcv_array);
				foreach ($objectList as $object)
				{
					$object->Delete($deep, $across);
				}
			}
			else
			{
				$connection = Database::Connect();
				$pog_query = "delete from `wiki_page` where ";
				for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
				{
					if (sizeof($fcv_array[$i]) == 1)
					{
						$pog_query .= " ".$fcv_array[$i][0]." ";
						continue;
					}
					else
					{
						if ($i > 0 && sizeof($fcv_array[$i-1]) !== 1)
						{
							$pog_query .= " AND ";
						}
						if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$this->Escape($fcv_array[$i][2])."'";
						}
						else
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$fcv_array[$i][2]."'";
						}
					}
				}
				return Database::NonQuery($pog_query, $connection);
			}
		}
	}
	
	
	/**
	* Associates the wiki_namespace object to this one
	* @return boolean
	*/
	function GetWiki_namespace()
	{
		$wiki_namespace = new wiki_namespace();
		return $wiki_namespace->Get($this->wiki_namespaceId);
	}
	
	
	/**
	* Associates the wiki_namespace object to this one
	* @return 
	*/
	function SetWiki_namespace(&$wiki_namespace)
	{
		$this->wiki_namespaceId = $wiki_namespace->wiki_namespaceId;
	}
	
	
	/**
	* Gets a list of views_on_page_at_tick objects associated to this one
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array of views_on_page_at_tick objects
	*/
	function GetViews_on_page_at_tickList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$views_on_page_at_tick = new views_on_page_at_tick();
		$fcv_array[] = array("wiki_pageId", "=", $this->wiki_pageId);
		$dbObjects = $views_on_page_at_tick->GetList($fcv_array, $sortBy, $ascending, $limit);
		return $dbObjects;
	}
	
	
	/**
	* Makes this the parent of all views_on_page_at_tick objects in the views_on_page_at_tick List array. Any existing views_on_page_at_tick will become orphan(s)
	* @return null
	*/
	function SetViews_on_page_at_tickList(&$list)
	{
		$this->_views_on_page_at_tickList = array();
		$existingViews_on_page_at_tickList = $this->GetViews_on_page_at_tickList();
		foreach ($existingViews_on_page_at_tickList as $views_on_page_at_tick)
		{
			$views_on_page_at_tick->wiki_pageId = '';
			$views_on_page_at_tick->Save(false);
		}
		$this->_views_on_page_at_tickList = $list;
	}
	
	
	/**
	* Associates the views_on_page_at_tick object to this one
	* @return 
	*/
	function AddViews_on_page_at_tick(&$views_on_page_at_tick)
	{
		$views_on_page_at_tick->wiki_pageId = $this->wiki_pageId;
		$found = false;
		foreach($this->_views_on_page_at_tickList as $views_on_page_at_tick2)
		{
			if ($views_on_page_at_tick->views_on_page_at_tickId > 0 && $views_on_page_at_tick->views_on_page_at_tickId == $views_on_page_at_tick2->views_on_page_at_tickId)
			{
				$found = true;
				break;
			}
		}
		if (!$found)
		{
			$this->_views_on_page_at_tickList[] = $views_on_page_at_tick;
		}
	}
	
	
	/**
	* Gets a list of diff_record objects associated to this one
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array of diff_record objects
	*/
	function GetDiff_recordList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$diff_record = new diff_record();
		$fcv_array[] = array("wiki_pageId", "=", $this->wiki_pageId);
		$dbObjects = $diff_record->GetList($fcv_array, $sortBy, $ascending, $limit);
		return $dbObjects;
	}
	
	
	/**
	* Makes this the parent of all diff_record objects in the diff_record List array. Any existing diff_record will become orphan(s)
	* @return null
	*/
	function SetDiff_recordList(&$list)
	{
		$this->_diff_recordList = array();
		$existingDiff_recordList = $this->GetDiff_recordList();
		foreach ($existingDiff_recordList as $diff_record)
		{
			$diff_record->wiki_pageId = '';
			$diff_record->Save(false);
		}
		$this->_diff_recordList = $list;
	}
	
	
	/**
	* Associates the diff_record object to this one
	* @return 
	*/
	function AddDiff_record(&$diff_record)
	{
		$diff_record->wiki_pageId = $this->wiki_pageId;
		$found = false;
		foreach($this->_diff_recordList as $diff_record2)
		{
			if ($diff_record->diff_recordId > 0 && $diff_record->diff_recordId == $diff_record2->diff_recordId)
			{
				$found = true;
				break;
			}
		}
		if (!$found)
		{
			$this->_diff_recordList[] = $diff_record;
		}
	}
}
?>